package com.music.music.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.music.common.core.utils.SnowflakeIdWorker;
import com.music.music.domain.MusicSinger;
import com.music.music.domain.Singer;
import com.music.music.service.MusicSingerService;
import com.music.music.vo.MusicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.music.common.log.annotation.Log;
import com.music.common.log.enums.BusinessType;
import com.music.common.security.annotation.RequiresPermissions;
import com.music.music.domain.Music;
import com.music.music.service.MusicService;
import com.music.common.core.web.controller.BaseController;
import com.music.common.core.web.domain.AjaxResult;
import com.music.common.core.utils.poi.ExcelUtil;
import com.music.common.core.web.page.TableDataInfo;

/**
 * 音乐列表Controller
 *
 * @author ruoyi
 * @date 2024-10-22
 */
@RestController
@RequestMapping("/music")
public class MusicController extends BaseController {
    @Resource
    private MusicService musicService;
    @Resource
    private MusicSingerService musicSingerService;

    /**
     * 查询音乐列表列表
     */
    @RequiresPermissions("music:music:list")
    @GetMapping("/list")
    public TableDataInfo list(Music music) {
        startPage();
        List<MusicVo> list = musicService.selectMusicList(music);
        return getDataTable(list);
    }

    /**
     * 导出音乐列表列表
     */
    @RequiresPermissions("music:music:export")
    @Log(title = "音乐列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Music music) {
        List<MusicVo> list = musicService.selectMusicList(music);
        ExcelUtil<MusicVo> util = new ExcelUtil<MusicVo>(MusicVo.class);
        util.exportExcel(response, list, "音乐列表数据");
    }

    /**
     * 获取音乐列表详细信息
     */
    @RequiresPermissions("music:music:query")
    @GetMapping(value = "/{musicId}")
    public AjaxResult getInfo(@PathVariable("musicId") Long musicId) {
        return success(musicService.selectMusicByMusicId(musicId));
    }

    /**
     * 新增音乐列表
     */
    @Transactional
    @RequiresPermissions("music:music:add")
    @Log(title = "音乐列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MusicVo musicVo) {
        Long musicId = SnowflakeIdWorker.generateId();
        musicVo.setMusicId(musicId);
        int i = musicService.insertMusic(musicVo);
        List<Long> singerIds = musicVo.getWebSingerIds();
        singerIds.forEach(singerId -> musicSingerService.insertMusicSinger(new MusicSinger(musicId, singerId)));
        return toAjax(i);
    }

    /**
     * 修改音乐列表
     */
    @Transactional
    @RequiresPermissions("music:music:edit")
    @Log(title = "音乐列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MusicVo musicVo) {
        int i = musicService.updateMusic(musicVo);
        Long musicId = musicVo.getMusicId();
        musicSingerService.deleteMusicSingerByMusicId(musicId);
        List<Long> singerIds = musicVo.getWebSingerIds();
        singerIds.forEach(singerId -> musicSingerService.insertMusicSinger(new MusicSinger(musicId, singerId)));
        return toAjax(i);
    }

    /**
     * 删除音乐列表
     */
    @Transactional
    @RequiresPermissions("music:music:remove")
    @Log(title = "音乐列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{musicIds}")
    public AjaxResult remove(@PathVariable Long[] musicIds) {
        int i = musicService.deleteMusicByMusicIds(musicIds);
        musicSingerService.deleteMusicSingerByMusicIds(musicIds);
        return toAjax(i);
    }


    @RequestMapping("/feign/findMusicNameById")
    public String findMusicNameById(@RequestParam("musicId") Long musicId) {
        return musicService.findMusicNameById(musicId);
    }
}
